use "Trans Athletes DATA.dta"
gen stateid=.
replace stateid=1 if bill_number==391
replace stateid=2 if bill_number==1165
replace stateid=3 if bill_number==354
replace stateid=4 if bill_number==1028
replace stateid=5 if bill_number==500
replace stateid=6 if bill_number==1041
replace stateid=7 if bill_number==2416
replace stateid=8 if bill_number==55
replace stateid=9 if bill_number==83
replace stateid=10 if bill_number==156
replace stateid=11 if bill_number==2536
replace stateid=12 if bill_number==112
replace stateid=13 if bill_number==1298
replace stateid=14 if bill_number==2
replace stateid=15 if bill_number==972
replace stateid=16 if bill_number==4608
replace stateid=17 if bill_number==46
replace stateid=18 if bill_number==228
replace stateid=19 if bill_number==25
replace stateid=20 if bill_number==11
replace stateid=21 if bill_number==3293
replace stateid=22 if bill_number==435
replace stateid=23 if bill_number==151
replace stateid=24 if bill_number==133
replace stateid=25 if bill_number==196
replace stateid=26 if bill_number==574
replace stateid=27 if bill_number==39
replace stateid=28 if bill_number==1387
*stateid *Alabama = 1; Arizona = 2; Arkansas = 3; Florida = 4; Idaho = 5; Indiana = 6; Iowa = 7; Kansas = 8; Kentucky = 9; Louisiana = 10; Mississippi = 11; Montana = 12; North Dakota = 13; Oklahoma = 14; Pennsylvania = 15; South Carolina = 16; South Dakota = 17; Tennessee = 18; Texas = 19; Utah = 20; West Virginia = 21; Georgia = 22; Ohio = 23; Wyoming = 24; Wisconsin = 25; North Carolina = 26; Missouri = 27; Virginia = 28
label var vote_yes "binary outcome variable"
label var vote_no "binary outcome variable"
label var stateid "level-2 identifier"
label var senate "Senate or House"
label var district "Legislator district #"
label var mmd "Multimember District Indicator (0,1)"
label var term_length "Length of Term for Chamber"
label var district_magnitude "Number of Representatives in District"
label var lawmaker_name "Lawmaker's Name"
label var lgbt "Lawmaker identifies as LGBT"
label var republican "Republican Indicator for Lawmaker (0,1)"
label var democrat "Democratic Indicator for Lawmaker (0,1)" 
label var female "Female Indicator for Lawmaker (0,1)"
label var black "Black Indicator for Lawmaker (0,1)"
label var hispanic "Hispanic Indicator for Lawmaker (0,1)"
label var nativeamasian "Native American or Asian Indicator for Lawmaker (0,1)"
label var legis_ideal "ShorMcCarty Ideology Score"
label var election_year "Lawmaker's Election Year"
label var c_votes "Number of Votes Lawmaker Earned in Last Election"
label var dist_votes "Number of Votes for all Candidates in Last Election"
label var d_votes_floser "Number of Votes Earned by Losing Candidate with Most Votes"
label var margin_of_victory "Win Margin(c_votes-d_votes_floser)/(dist_votes/dist_magnitude)"
label var district_ideal "Warshaw District Ideology Estimates"
label var under25 "Percentage of Population Under 25 Years of Age"
label var age25_44 "Percentage of Population between 25 and 44 Years of Age"
label var age45_64 "Percentage of Population between 45 and 64 Years of Age"
label var pctover65 "Percentage of Population over 65 Years of Age"
label var pctwhite "Percentage of white population in district"
label var pctblack "Percentage of black population in district"
label var pcthispanic "Percentage of hispanic population in district"
label var pctnonwhite "Percentage of nonwhite population in district (black+hispanic+other)"
label var pctmarried "Percentage of District Population Married"
label var pctnevermarried "Percentage of District Population Never Married"
label var pcths "Percentage of District Population w High School Degree"
label var pctbachelordeg "Percentage of District Population with (at least) Bachelor's Degree"
label var urban "Percentage of District Population Living in Urban Area"
label var rural "Percentage of District Population Living in Rural Area"
*Of IVs, continuous = Legis_Ideal Margin_of_Victory District_Ideal pctover65 pctNonwhite pctNeverMarried pctBachelorDeg pctRural
*Only categorical (dichotomous) / binary = Republican Female Black
egen zsupermajority60 = std(supermajority60)
egen zsupermajority66 = std(supermajority66)
* Standardizing continuous IVs
egen zmargin_of_victory = std(margin_of_victory)
egen zdistrict_ideal = std(district_ideal)
egen zpctover65 = std(pctover65)
egen zpctnonwhite = std(pctnonwhite)
egen zpctnevermarried = std(pctnevermarried)
egen zpctbachelordeg = std(pctbachelordeg)
egen zrural = std(rural)
egen zlegis_ideal = std(legis_ideal)
egen zSSMC = std(SSMC)
egen zAllRelig = std(AllRelig)
egen zEP = std(EP)
*Standardizing categorical / binary IVs
egen zrepublican = std(republican)
egen zfemale = std(female)
egen zblack = std(black)
egen zlgbt = std(lgbt)
* % vote_yes by party, pg3
tab vote_yes if republican
tab vote_yes if democrat
* % "yes" votes cast in favor of legislation, pg 6
tab vote_yes
*District ideology score range, pg 7
sum district_ideal
*Margin of victory score range, mean, SD pg 8
sum margin_of_victory 

*Building the regression model
*Standardized and unstandardized logistic regression for voting "yes" and voting "no"
logistic vote_yes republican supermajority66 female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural
logistic vote_yes zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural
logistic vote_no republican supermajority66 female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural
logistic vote_no zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural
*Significant vote_yes variables: republican, supermajority66, female, lgbt, district ideology, % over 65, SSMC, % bachelor's degree, EP.

*Building an empty model, no predictors, calculate intraclass correlation coefficient degree of homogeneity of the outcome within clusters
melogit vote_yes || stateid:
estat icc
*N=3654 legislators nested in K = 28 states
*The fixed intercept is B00 = .86;
*Random intercept variance (level 2 residual) var(u0j) .1450916
*The ICC is = .042 or .04 ; Only 4% of the chance that someone will vote yes is explained by between-state differences. 96% is explained by within state differences.

***Add clustering by state****
melogit vote_yes republican supermajority66 female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural || stateid:
melogit vote_yes republican supermajority66 female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural || stateid:, or
*Models with standardized IVs, Table 1
melogit vote_yes zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:
melogit vote_yes zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:, or
*Yes if Republican, omit black 
melogit vote_yes zsupermajority66 zfemale zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if republican==1|| stateid:
melogit vote_yes zsupermajority66 zfemale zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if republican==1|| stateid:, or
estat ic
*Yes if Democratic, omit lgbt
melogit vote_yes zsupermajority66 zfemale zblack zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if democrat==1 || stateid:
estat ic 
*AIC - BIC 
estat ic
estat icc 

*Figure 1 Likelihood of a yes vote by party ID and ed attainment. 
*Probabilities by gender, % bachelors degree
gen pctbachcategory=.
replace pctbachcategory=1 if pctbachelordeg<20
replace pctbachcategory=2 if pctbachelordeg>=20 & pctbachelordeg<40
replace pctbachcategory=3 if pctbachelordeg>=40 & pctbachelordeg<60
replace pctbachcategory=4 if pctbachelordeg>=60 & pctbachelordeg<100
tab pctbachcategory 
*Probabilities by party, bachelors degree, w fixed effects 
melogit vote_yes i.republican pctbachcategory || stateid:
margins republican, at (pctbachcategory=(1(1)4)) predict (mu fixedonly) vsquish
marginsplot
*Probabilities by party, bachelors degree, all variables
melogit vote_yes i.republican female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC EP pctbachcategory urban  || stateid:
margins republican, at (pctbachcategory=(1(1)4)) predict (mu fixedonly) vsquish
marginsplot

*Table 2 EDIT -Legislator across party lines*
*Democrats
sum margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural if democrat==1 & vote_yes==0
sum margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural if democrat==1 & vote_yes==1
*Republicans EDIT 
sum margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural if republican==1 & vote_yes==0
sum margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural if republican==1 & vote_yes==1

*Table 2 Legislator across party lines*
*Democrats
sum margin_of_victory if democrat==1 & vote_yes==0
sum margin_of_victory if democrat==1 & vote_yes==1
sum district_ideal if democrat==1 & vote_yes==1
sum district_ideal if democrat==1 & vote_yes==0
sum pctover65 if democrat==1 & vote_yes==0
sum pctover65 if democrat==1 & vote_yes==1
sum pctnonwhite if democrat==1 & vote_yes==1
sum pctnonwhite if democrat==1 & vote_yes==0
sum SSMC if democrat==1 & vote_yes==0
sum SSMC if democrat==1 & vote_yes==1
sum pctbachelordeg if democrat==1 & vote_yes==1
sum pctbachelordeg if democrat==1 & vote_yes==0
sum EP if democrat==1 & vote_yes==1
sum EP if democrat==1 & vote_yes==0
sum rural if democrat==1 & vote_yes==0
sum rural if democrat==1 & vote_yes==1
*Republicans
sum margin_of_victory if republican==1 & vote_yes==0
sum margin_of_victory if republican==1 & vote_yes==1
sum district_ideal if republican==1 & vote_yes==1
sum district_ideal if republican==1 & vote_yes==0
sum pctover65 if republican==1 & vote_yes==0
sum pctover65 if republican==1 & vote_yes==1
sum pctnonwhite if republican==1 & vote_yes==1
sum pctnonwhite if republican==1 & vote_yes==0
sum SSMC if republican==1 & vote_yes==0
sum SSMC if republican==1 & vote_yes==1
sum pctbachelordeg if republican==1 & vote_yes==1
sum pctbachelordeg if republican==1 & vote_yes==0
sum EP if republican==1 & vote_yes==1
sum EP if republican==1 & vote_yes==0
sum rural if republican==1 & vote_yes==0
sum rural if republican==1 & vote_yes==1

*Voting against party preferences. Florida=4 LA= 10, ND = 13 - data w/ table 2
tab vote_yes if democrat==1 & stateid==4
tab vote_yes if democrat==1 & stateid==10
tab vote_yes if republican==1 & stateid==13
tab vote_yes if republican==1
tab vote_yes if democrat==1

*t values 
ttest district_ideal if republican==1, by (vote_yes)
ttest EP if republican==1, by (vote_yes)
ttest pctbachelordeg if republican==1, by (vote_yes)
ttest pctbachelordeg if democrat==1, by (vote_yes)
ttest rural if democrat==1, by (vote_yes)

*-----ONLINE APPENDIX------------

*Appendix Table 1 w/ and wo ideology
melogit vote_yes legis_ideal republican supermajority66 female black lgbt margin_of_victory district_ideal pctover65 pctnonwhite SSMC pctbachelordeg EP rural || stateid:
melogit vote_yes zlegis_ideal zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:
melogit vote_yes zlegis_ideal zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:, or
melogit vote_yes zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:
melogit vote_yes zrepublican zsupermajority66 zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural || stateid:, or

*Appendix Figure 1  Legis Ideal summary table of Mean & StDev
sum legis_ideal if republican==1 & vote_yes==1
sum legis_ideal if republican==1 & vote_yes==0
sum legis_ideal if democrat==1 & vote_yes==1
sum legis_ideal if democrat==1 & vote_yes==0
sum legis_ideal if black==1 & vote_yes==1
sum legis_ideal if black==1 & vote_yes==0
sum legis_ideal if lgbt==1 & vote_yes==1
sum legis_ideal if lgbt==1 & vote_yes==0
sum legis_ideal if republican==1 & female==1 & vote_yes==1
sum legis_ideal if republican==1 & female==1 & vote_yes==0

*Generate minority variable
gen minority = 0
replace minority = 1 if black==1
replace minority = 1 if hispanic==1
replace minority = 1 if nativeamasian==1

*Generate legislator is white variable
gen whitelegis = 1
replace whitelegis = 0 if black==1
replace whitelegis = 0 if hispanic==1
replace whitelegis = 0 if nativeamasian==1
tab whitelegis

*Generate interaction term, black legislator with high proportion of EP
gen blackxEP = black * EP

*Appendix Table #2 
tab vote_yes if republican==1
tab vote_yes if democrat==1
tab vote_yes if independent==1
tab vote_yes if female==1
tab vote_yes if female==0
tab vote_yes if lgbt==1
tab vote_yes if black==1
tab vote_yes if minority==1
tab vote_yes if black==1 & female==0
tab vote_yes if black==1 & female==1
tab vote_yes if black==1 & female==0 & democrat==1
tab vote_yes if black==1 & female==1 & democrat==1
tab vote_yes if blackxEP>200 & black==1
tab vote_yes if republican==1 & female==1
tab vote_yes if democrat==1 & female==1
tab vote_yes if republican==1 & female==0
tab vote_yes if republican==0 & female==0
tab vote_yes if female==1 & whitelegis==1
tab vote_yes if female==1 & black==1
tab vote_yes if female==1 & minority==1

*Appendix Arkansas = 3; Florida = 4; Idaho = 5; Indiana = 6; Iowa = 7; Kansas = 8; Kentucky = 9; Louisiana = 10; Mississippi = 11; Montana = 12; North Dakota = 13; Oklahoma = 14; Pennsylvania = 15; South Carolina = 16; South Dakota = 17; Tennessee = 18; Texas = 19; Utah = 20; West Virginia = 21; Georgia = 22; Ohio = 23; Wyoming = 24; Wisconsin = 25; North Carolina = 26; Missouri = 27; Virginia = 28
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==3
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==4
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==5
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==6
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==7
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==8
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==9
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==10
logit vote_yes zfemale zblack zlgbt margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==27
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==12
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==13
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==23
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==14
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==15
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==16
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==17
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==18
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==19
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==20
logit vote_yes zfemale zblack margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==21
logit vote_yes zfemale margin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zSSMC zpctbachelordeg zEP zrural if stateid==24

*-------------------

*Does context matter? Bills include trans boys provisions
logit vote_yes zrepublican zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zpctnevermarried zpctbachelordeg zrural if stateid==1 | stateid==22 | stateid==10 |stateid==13 | stateid==23 | stateid==16
*Does not include trans boys provisions
logit vote_yes zrepublican zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zpctnevermarried zpctbachelordeg zrural if stateid==2 | stateid==3 | stateid==4 |stateid==5 | stateid==6 | stateid==7 | stateid==8 | stateid==9 | stateid==11 | stateid==12 | stateid==14 | stateid==15 | stateid==17 | stateid==18 | stateid==19 | stateid==20 | stateid==21 | stateid==24 | stateid==25 
*Bills that require birth certificate
logit vote_yes zrepublican zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zpctnevermarried zpctbachelordeg zrural if stateid==4 | stateid==22 | stateid==6 |stateid==7 | stateid==9 | stateid==13 | stateid==14 | stateid==16 | stateid==17 | stateid==18 | stateid==19 | stateid==20 | stateid==21 | stateid==24
*Does not require birth certificate
logit vote_yes zrepublican zfemale zblack zlgbt zmargin_of_victory zdistrict_ideal zpctover65 zpctnonwhite zpctnevermarried zpctbachelordeg zrural if stateid==1 | stateid==2 | stateid==3 |stateid==5 | stateid==8 | stateid==10 | stateid==11 | stateid==12 | stateid==15 | stateid==22 | stateid==23 | stateid==25

ttest margin_of_victory if republican==1, by (vote_yes)
ttest district_ideal , by (vote_yes)
ttest pctover65 , by (vote_yes)
ttest pctnonwhite , by (vote_yes)
ttest pctnevermarried , by (vote_yes)
ttest pctbachelordeg , by (vote_yes)
ttest rural , by (vote_yes) 



